package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.Room;
import com.the4man.framework.util.PageJson;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

@Action(
        value = "locAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/room/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/room/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/room/edit.jsp"
        )}
)
public class LocAction extends BaseAction implements ModelDriven<Room> {
    private Room room = new Room();

    public LocAction() {
    }

    public void loc() {
        PageJson json = new PageJson();

        try {
            String str = this.getRequest().getParameter("id");
            json.setReturnMsg("res：" + str);
            json.setReturnFlag(true);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(sdf.format(new Date()));
            int size = this.getRequest().getContentLength();
            System.out.println(size);
            InputStream is = this.getRequest().getInputStream();
            byte[] reqBodyBytes = readBytes(is, size);
            String res = new String(reqBodyBytes);
            System.out.println(res);
        } catch (Exception var8) {
            json.setReturnMsg("出现异常！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public static final byte[] readBytes(InputStream is, int contentLen) {
        if(contentLen > 0) {
            int readLen = 0;
//            int readLengthThisTime = false;
            byte[] message = new byte[contentLen];

            try {
                while(readLen != contentLen) {
                    int readLengthThisTime = is.read(message, readLen, contentLen - readLen);
                    if(readLengthThisTime == -1) {
                        break;
                    }

                    readLen += readLengthThisTime;
                }

                return message;
            } catch (IOException var6) {
                ;
            }
        }

        return new byte[0];
    }

    public static void main1(String[] args) {
        byte[] bs = hexStr2Bytes("d6");
        System.out.println(bs);
    }

    public static void main(String[] args) {
        String hexStr = "0000000000000000cc830000d800015400070000cc830000d6000028d468ba05050600003a590a9df9c202010200000000000002b4a70003000000000000000000000000cc830000d6000028d468ba0505060000a43e137ba54202010200000000000002b4a70002000000000000000000000000cc830000d6000028d468ba05050600003499f8b66f5902010200000000000002b5a70002000000000000000000000000cc830000d6000028d468ba0505060000365fcfed14f202010200000000000002b7a70002000000000000000000000000BBcc830000d800015400070000cc830000d6000028d468ba05050600003a590a9df9c202010200000000000002b4a70003000000000000000000000000cc830000d6000028d468ba0505060000a43e137ba54202010200000000000002b4a70002000000000000000000000000cc830000d6000028d468ba05050600003499f8b66f5902010200000000000002b5a70002000000000000000000000000cc830000d6000028d468ba0505060000365fcfed14f202010200000000000002b7a70002000000000000000000000000EEFFAA0000000000000000cc830000d800015400070000cc830000d6000028d468ba05050600003a590a9df9c202010200000000000002b4a70003000000000000000000000000cc830000d6000028d468ba0505060000a43e137ba54202010200000000000002b4a70002000000000000000000000000cc830000d6000028d468ba05050600003499f8b66f5902010200000000000002b5a70002000000000000000000000000cc830000d6000028d468ba0505060000365fcfed14f202010200000000000002b7a70002000000000000000000000000BBcc830000d800015400070000cc830000d6000028d468ba05050600003a590a9df9c202010200000000000002b4a70003000000000000000000000000cc830000d6000028d468ba0505060000a43e137ba54202010200000000000002b4a70002000000000000000000000000cc830000d6000028d468ba05050600003499f8b66f5902010200000000000002b5a70002000000000000000000000000cc830000d6000028d468ba0505060000365fcfed14f202010200000000000002b7a70002000000000000000000000000EEFFAA";
        byte[] bs = hexStr2Bytes(hexStr);
//        byte[] tmp = null;
//        byte[] left = null;
        byte[] tmp = Arrays.copyOf(bs, bs.length);
        String apMac = null;
        String eqMac = null;

        for(int i = 0; i < tmp.length; ++i) {
            if(tmp.length - i < 48) {
                byte[] left = new byte[tmp.length - i];

                for(int j = 0; j < left.length; ++j) {
                    left[j] = tmp[i + j];
                }

                System.out.println("left bytes:" + byte2HexStr(left));
                break;
            }

            if(tmp[i] == -52 && tmp[i + 1] == -125 && tmp[i + 4] == -42) {
                byte[] apMac_bs = new byte[6];
                byte[] eqMac_bs = new byte[6];

                for(int j = 0; j < 6; ++j) {
                    apMac_bs[j] = tmp[i + 8 + j];
                    eqMac_bs[j] = tmp[i + 16 + j];
                }

                apMac = byte2HexStr(apMac_bs);
                eqMac = byte2HexStr(eqMac_bs);
                System.out.println("apmac:" + apMac);
                System.out.println("eqmac:" + eqMac);
                i += 47;
            }
        }

    }

    public static byte[] toStringHex(String s) {
        byte[] baKeyword = new byte[s.length() / 2];

        for(int i = 0; i < baKeyword.length; ++i) {
            try {
                baKeyword[i] = (byte)(255 & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception var4) {
                var4.printStackTrace();
            }
        }

        return baKeyword;
    }

    public static String byte2HexStr(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            if(stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }

        return hs.toUpperCase();
    }

    public static byte[] hexStr2Bytes(String src) {
//        int m = false;
//        int n = false;
        int l = src.length() / 2;
        byte[] ret = new byte[l];

        for(int i = 0; i < l; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            ret[i] = uniteBytes(src.substring(i * 2, m), src.substring(m, n));
        }

        return ret;
    }

    private static byte uniteBytes(String src0, String src1) {
        byte b0 = Byte.decode("0x" + src0).byteValue();
        b0 = (byte)(b0 << 4);
        byte b1 = Byte.decode("0x" + src1).byteValue();
        byte ret = (byte)(b0 | b1);
        return ret;
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        return "addPage";
    }

    public String toEditPage() {
        return "editPage";
    }

    public void getAllDatasourceList() {
    }

    public void getFullDatasourceList() {
    }

    public void getRoomSelectData() {
    }

    public void add() {
    }

    public void update() {
    }

    public void getBedInfoList() {
    }

    public Room getModel() {
        return this.room;
    }
}
